﻿using System;
using System.Web.UI.WebControls;
using PowerToolsWeb.DataAccess;
using PowerToolsWeb.Facade;

namespace PowerToolsWeb.Admin
{
    public partial class TypeList : AdminPageBase
    {
        public int ParantTypeId
        {
            get
            {
                if (Request.QueryString["ParantTypeId"] == null)
                {
                    return 0;
                }
                else
                {
                    int ptid = 0;
                    int.TryParse(Request.QueryString["ParantTypeId"].ToString(), out ptid);
                    return ptid;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindType();
                spannal.InnerHtml = (ParantTypeId == 0) ? "所有类别列表：一级类别" : "所有类别列表：<a href='TypeList.aspx'>一级类别</a> —— 二级类别";
            }
        }

        void BindType()
        {
            Tpt_ProducttypeCollection listType = new Tpt_ProducttypeCollection();
            listType.ListTypeByParantId(ParantTypeId);
            GridView1.DataSource = listType.DataTable;
            GridView1.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            SaveType(0);
        }
        //保存类别
        void SaveType(int typeid)
        {
            if (this.txbTypeName.Text == "")
            {
                Alert(Page, "请输入类别名");
                return;
            }

            Tpt_Producttype saveType = new Tpt_Producttype();
            if (typeid == 0)
            {
                Tpt_Producttype haveType = new Tpt_Producttype();
                if (haveType.GetByTypeName(txbTypeName.Text.Trim(), ParantTypeId))
                {
                    Alert(Page, "该类别已存在");
                    return;
                }
                saveType.TYPENAME = txbTypeName.Text.Trim();
                saveType.PARANTTYPEID = ParantTypeId;
                if (!saveType.Insert())
                {
                    Alert(Page, "添加类别出错");
                    return;
                }
                this.txbTypeName.Text = "";
                Alert(Page, "添加类别成功");
            }
            else
            {
                if (!saveType.SelectByPk(typeid))
                {
                    Alert(Page, "查询该类别出错");
                    return;
                }
                saveType.TYPENAME = txbTypeName.Text.Trim();
                if (!saveType.Update())
                {
                    Alert(Page, "更新类别出错");
                    return;
                }
                Alert(Page, "更新类别成功");
            }
            BindType();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string commandName = e.CommandName;
            switch (commandName)
            {
                case "inDown": Response.Redirect("TypeList.aspx?ParantTypeId=" + e.CommandArgument); break;
                case "deleteType":
                    int typeid = 0;
                    if (!int.TryParse(e.CommandArgument.ToString(), out typeid))
                    {
                        Alert(Page, "类别编号有误");
                        return;
                    }
                    Tpt_Producttype deleteType = new Tpt_Producttype();
                    if (deleteType.GetChildAndProductCount(typeid))
                    {
                        int _count = 0;
                        if (!int.TryParse(deleteType.DataRow["_count"].ToString(), out _count))
                        {
                            Alert(Page, "删除失败！（查询该类别的下级类别或拥有产品数有误）");
                            return;
                        }
                        if (_count > 0)
                        {
                            Alert(Page, "该类别拥有下级类别，或已有该类别的产品，不能删除。\n若要删除,请先删除它的下级或该类别的产品。");
                            return;
                        }
                    }
                    else
                    {
                        Alert(Page, "删除失败！（查询该类别的下级类别或拥有产品数有误）");
                        return;
                    }
                    if (!deleteType.Delete(typeid))
                    {
                        Alert(Page, "删除类别出错");
                    }
                    else
                    {
                        Alert(Page, "删除类别成功");
                        BindType();
                    }
                    break;
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GridView1.EditIndex = e.NewEditIndex;
            BindType();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GridView1.EditIndex = -1;
            BindType();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                Tpt_Producttype type = new Tpt_Producttype();
                if (!type.SelectByPk(int.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString())))
                {
                    Alert(Page, "类别有误");
                    return;
                }
                string newTypeName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString().Trim();
                if (newTypeName == "")
                {
                    Alert(Page, "请输入类别名");
                    return;
                }
                type.TYPENAME = newTypeName;
                if (!type.Update())
                {
                    Alert(Page, "修改失败");
                    return;
                }
                this.GridView1.EditIndex = -1;
                BindType();
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex > -1)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E8E6EA'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }
    }
}
